<!-- xxtea 加载密文 -->
{{- $url := .Get "url" -}}
{{- $md5Hash := md5 $url -}}
{{- $md5Short := substr $md5Hash 0 8 -}}

<div id="{{- $md5Short -}}-bar" style="display: none;">
    <input id="password" placeholder="请输入口令..."></input>
    <div><button id="submitBtn">解密</button></div>
</div>
<div id="{{- $md5Short -}}">
</div>
<script type="text/javascript" src="https://cdn.ftls.xyz/js/crypto/xxtea.js"></script>
<script>
    const msg = document.getElementById("{{- $md5Short -}}");
    const submitBtn = document.getElementById("submitBtn");
    let newText = "";
    function getNewText() {
        fetch("{{ $url }}")
            .then(response => response.text())
            .then(text => {
                console.log("加载密文: " + text);
                msg.innerHTML = "已加载密文";
                newText = text;
                document.getElementById("{{- $md5Short -}}-bar").style.display = "block";
            });
    }
    getNewText();
    submitBtn.addEventListener("click", () => {
        const password = document.getElementById("password").value;
        console.log("口令: " + password, "密文: " + newText);
        try {
            let decrypt_data = XXTEA.decryptFromBase64(newText.trim(), password);
            msg.innerHTML = decrypt_data;
        } catch (error) {
            msg.innerHTML = "口令错误";
        }
    });
</script>